/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrador
 */
public class CurrencyFormaterTest {
    private JLabel jl;
    private JFormattedTextField jft, jft1, jft2, jft3, jft4;
    private JButton jb = new JButton("but");

    public CurrencyFormaterTest() {
        JFrame frame = new JFrame("Text Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jl = new JLabel("Try with formatted text field   ");
        Locale local1 = new Locale("en", "US");
        int minFra = 0;
        int maxFra = 3;
        jft = setFormat(jft, local1, minFra, maxFra);
        //jft.setColumns(20);
        jft.setMaximumSize(new Dimension(100, 20));
        jft.setMinimumSize(new Dimension(100, 20));
        jft.setPreferredSize(new Dimension(100, 20));
        local1 = new Locale("gb", "GB");
        minFra = 2;
        maxFra = 2;
        jft1 = setFormat(jft1, local1, minFra, maxFra);
        //jft1.setColumns(20);
        jft1.setMaximumSize(new Dimension(100, 20));
        jft1.setMinimumSize(new Dimension(100, 20));
        jft1.setPreferredSize(new Dimension(100, 20));
        local1 = new Locale("de", "DE");
        minFra = 0;
        maxFra = 3;
        jft2 = setFormat(jft2, local1, minFra, maxFra);
        //jft2.setColumns(20);
        jft2.setMaximumSize(new Dimension(100, 20));
        jft2.setMinimumSize(new Dimension(100, 20));
        jft2.setPreferredSize(new Dimension(100, 20));
        local1 = new Locale("jp", "JP");
        minFra = 0;
        maxFra = 0;
        jft3 = setFormat(jft3, local1, minFra, maxFra);
        //jft3.setColumns(20);
        jft3.setMaximumSize(new Dimension(100, 20));
        jft3.setMinimumSize(new Dimension(100, 20));
        jft3.setPreferredSize(new Dimension(100, 20));
        local1 = new Locale("hk", "HK");
        minFra = 0;
        maxFra = 3;
        jft4 = setFormat(jft4, local1, minFra, maxFra);
        //jft4.setColumns(20);
        jft4.setMaximumSize(new Dimension(100, 20));
        jft4.setMinimumSize(new Dimension(100, 20));
        jft4.setPreferredSize(new Dimension(100, 20));
        frame.add(jb);
        //
        frame.add(jl);
        frame.add(jft, BorderLayout.NORTH);
        frame.add(jft1, BorderLayout.SOUTH);
        frame.add(jft2, BorderLayout.WEST);
        frame.add(jft3, BorderLayout.EAST);
        frame.add(jft4, BorderLayout.CENTER);
        jb.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(jb, "nel text Box : " + jft.getText());
            }
        });

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        CurrencyFormaterTest textTest = new CurrencyFormaterTest();
    }

    private JFormattedTextField setFormat(JFormattedTextField jft, Locale local1, int minFra, int maxFra) {
        NumberFormat numberFormat;
        Locale local = local1;
        int setMin = minFra;
        int setMax = maxFra;
        numberFormat = NumberFormat.getCurrencyInstance(local);
        numberFormat.setMinimumFractionDigits(setMin);
        numberFormat.setMaximumFractionDigits(setMax);
        numberFormat.setRoundingMode(RoundingMode.HALF_UP);
        jft = new JFormattedTextField(numberFormat);
        jft.setValue(new Double(342.796));
        return jft;
    }
}
